#!/usr/bin/python
# -*- coding: UTF-8 -*-
import itchat
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plot
import pandas as pd
from io import StringIO
from utils.GwordClude import *
import jieba

plot.rcParams['font.sans-serif'] = ['SimHei']
plot.rcParams['font.family'] = 'sans-serif'


def get_freides():
    itchat.login()
    friends = itchat.get_friends(update=True)
    result = json.dumps(friends, ensure_ascii=False)
    with open("temp.txt", "w", encoding="utf-8")as f:
        f.write(result)
    print(friends)


result = ""
with open("temp.txt", "r", encoding="utf-8")as f:
    result = f.read()
data = pd.DataFrame(json.loads(result))
sig=pd.DataFrame(data,index=data.index,columns=["Signature"])
sig=sig["Signature"]


def get_job_requets(data):
    result=list(data)
    string=StringIO()
    for str in result:
        if str == None or str == "":
            continue
        string.write(str)

    return string.getvalue()

def get_cloud_word(data):
    all_words = get_job_requets(data)
    print(all_words)
    generate_cloud_word(jieba.cut(all_words), "../utils/cccc.jpg", "sig.jpg")


get_cloud_word(sig)
def city_show():
    global data
    data["City"] = data["City"].apply(lambda x: np.NaN if x == "" else x)
    data = data.fillna("未知")

    for index, row in data.iterrows():
        pass
    city = data.groupby(by="City").size()
    city = city.sort_values(ascending=False)
    city = city.head(18)
    plot.style.use("ggplot")
    bar_width = 0.3
    plot.figure(figsize=(16, 6))
    plot.title("骑象人好友所在城市分布")
    x = list(city.index)
    plot.xticks(np.arange(len(x)), x)
    plot.xlabel('城市')
    plot.ylabel("个数")
    p2 = plot.bar(np.arange(len(x)), city.values, bar_width)
    # plot.xticks(Rotation=45)
    plot.savefig("city.png")
    plot.show()




def sex():
    global data
    data = data.groupby(by="Sex").size()
    data = data / 214
    print(data)
    print(data.sum())
    plot.style.use("ggplot")
    labels = ["unkown", "male", "female"]
    fracs = [0.11, 0.66, 0.23]
    explode = (0, 0.05, 0)
    plot.axes(aspect=1)
    plot.title("骑象人微信联系人男女分布比例")
    # plt.pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True)
    plot.pie(fracs, explode=explode, labels=labels, autopct='%.0f%%', shadow=True)
    plot.legend(labels, loc="upper left", bbox_to_anchor=(1, 1))
    plot.savefig("male.png")
    plot.show()
